<template>
  <div class="container">
    <el-dialog title="新增目录" :visible="showDialog" width="30%" @close="onClose" @closed="reset">
      <el-form label-width="80px" :model="form" :rules="formRules" ref="formRef">
        <el-form-item label="所属学科" prop="subjectID" v-if="!subjects.id || !subjects.name">
          <el-select size="small" style="width:100%" v-model="form.subjectID">
            <el-option v-for="item in array" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input size="small" v-model="form.directoryName" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose">取 消</el-button>
        <el-button type="primary" @click="onConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 接口
import { add } from '@/api/hmmm/directorys.js'
export default {
  name: 'DirectorysAdd',
  data() {
    return {
      form: {
        subjectID: '',
        directoryName: ''
      },
      formRules: {
        subjectID: [{ required: true, message: '所属学科为必选项', trigger: 'change' }],
        directoryName: [{ required: true, message: '目录名称为必填项', trigger: 'blur' }]
      }
    }
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    array: {
      type: Array,
      required: true
    }
  },
  methods: {
    onClose() {
      this.$emit('update:showDialog', false)
    },
    onConfirm() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        await add(this.form)
        this.$message.success('操作成功！')
        this.$emit('reload')
        this.$emit('update:showDialog', false)
      })
    },
    reset() {
      this.$refs.formRef.resetFields()
    }
  },
  computed: {
    subjects() {
      return this.$route.query || null
    }
  },
  watch: {
    '$route.query'() {
      this.initList()
    }
  }
}
</script>

<style scoped lang="less"></style>
